/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import ru.miit.elearning.hibernate.dto.CurriculumLevel;
import ru.miit.elearning.hibernate.dto.UserIdentity;
import ru.miit.elearning.hibernate.dto.beans.SearchResult;

/**
 *
 * @author Cyclone
 */
public class GenericDAO {

    public GenericDAO(Session session) {
        this.session = session;
    }
    protected Session session = null;
    public static final short PROFILE_TYPE_STUDENT = 3, PROFILE_TYPE_TUTOR = 2, CURRICULUM_LEVEL_TYPE_LAB = 4, CURRICULUM_LEVEL_TYPE_COURSE = 2, CURRICULUM_LEVEL_TYPE_DIRECTION = 1;
    public static final int PROPERTY_TYPE_CURRICULUM = 1, PROPERTY_TYPE_USER = 2;
    public static final int LABORATORY = 1;

    public Integer findUserIdByLogin(String remoteUser) {
        UserIdentity userIdentity = (UserIdentity) (session.createQuery("from UserIdentity ui where ui.login = :remoteUser").setParameter("remoteUser", remoteUser).uniqueResult());
        if (userIdentity == null) {
            return null;
        }
        return userIdentity.getAccount();
    }

    public String findUserNameById(int userId) {
        return ((UserIdentity) session.createQuery("from UserIdentity ui where ui.account = :userId").setParameter("userId", userId).uniqueResult()).getName();
    }

    public boolean isUserInRoleStudent(int userId) {
        return isUserInRole(userId, PROFILE_TYPE_STUDENT);
    }

    public boolean isUserInRoleTutor(int userId) {
        return isUserInRole(userId, PROFILE_TYPE_TUTOR);
    }

    private boolean isUserInRole(int userId, short profileType) {
        return session.createQuery("from Profile where id.account = :userId "
                + "and id.id = :profileType").setParameter("userId", userId).setParameter("profileType", profileType).uniqueResult() == null ? false : true;
    }

    public List<SearchResult> search(String query) {
        List<SearchResult> resultList = new ArrayList<SearchResult>();
        try {
            List<UserIdentity> userList = session.createQuery("FROM UserIdentity WHERE upper(name) LIKE upper('%' || :query || '%') ORDER BY name").setParameter("query", query).list();
            for (UserIdentity ui : userList) {
                if (ui != null) {
                    SearchResult sr = new SearchResult();
                    sr.setName(ui.getName());
                    sr.setType("Пользователь");
                    sr.setLink("Profile?id=" + ui.getAccount());
                    resultList.add(sr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            List<CurriculumLevel> clList = session.createQuery("FROM CurriculumLevel WHERE upper(title) LIKE upper('%' || :query || '%') ORDER BY curriculumLevelType.id, title").setParameter("query", query).list();
            for (CurriculumLevel cl : clList) {
                if (cl != null) {
                    SearchResult sr = null;
                    switch (cl.getCurriculumLevelType().getId()) {
                        case CURRICULUM_LEVEL_TYPE_COURSE:
                            sr = new SearchResult();
                            sr.setType("Курс");
                            sr.setLink("Course?id=" + cl.getId());
                            break;
                        case CURRICULUM_LEVEL_TYPE_LAB:
                            sr = new SearchResult();
                            sr.setType("Практическая работа");
                            sr.setLink("Lab?id=" + cl.getId());
                            break;
                    }
                    if (sr != null) {
                        sr.setName(cl.getTitle());
                        resultList.add(sr);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }
}
